package com.thinksns.sociax.t4.android.popupwindow;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Build;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.unit.SociaxUIUtils;

/**
 * Created by zhiyicx on 2017/5/9.
 */

public class EventEditPopupWindow {
    private Activity activity;
    private EditText editText;
    private String title;
    private String hint;
    private OnPopConfirm onPopConfirm;
    private OnPopCancel onPopCancel;
    private OnDismissListener onDismissListener;
    private int animationStyle;
    private int width;
    private int height;
    private PopupWindow mPopupWindow;
    private TextView event_et_title;
    private TextView event_et_cancel_tv;
    private TextView event_et_confirm_tv;
    private TextView tv_content;
    private EditText event_et_et;
    private boolean isEdit;
    private String content;
    private String confirmText;
    private String cancelText;
    private int inputType;

    private EventEditPopupWindow(Builder builder){
        activity = builder.activity;
        title = builder.title;
        hint = builder.hint;
        onPopConfirm = builder.onPopConfirm;
        onPopCancel = builder.onPopCancel;
        onDismissListener = builder.onDismissListener;
        animationStyle = builder.animationStyle;
        width = builder.width;
        height = builder.height;
        inputType = builder.inputType;
        isEdit = builder.isEdit;
        content = builder.content;
        confirmText = builder.confirmText;
        cancelText = builder.cancelText;
        initPop();
    }

    private void initPop(){
        View view = LayoutInflater.from(activity).inflate(R.layout.event_edit_pop,null);
        event_et_title = (TextView) view.findViewById(R.id.event_et_title);
        event_et_cancel_tv = (TextView) view.findViewById(R.id.event_et_cancel_tv);
        event_et_confirm_tv = (TextView) view.findViewById(R.id.event_et_confirm_tv);
        event_et_et = (EditText) view.findViewById(R.id.event_et_et);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        if(isEdit){
            event_et_et.setVisibility(View.VISIBLE);
            tv_content.setVisibility(View.GONE);
        }else {
            event_et_et.setVisibility(View.GONE);
            tv_content.setVisibility(View.VISIBLE);
        }
        event_et_confirm_tv.setText(confirmText);
        event_et_cancel_tv.setText(cancelText);
        event_et_et.setInputType(inputType);
        event_et_title.setText(title);
        tv_content.setText(content);
        event_et_cancel_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onPopCancel!=null){
                    onPopCancel.onCancel();
                }
            }
        });
        event_et_confirm_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onPopConfirm!=null){
                    onPopConfirm.onConfirm(event_et_et.getText().toString());
                }
            }
        });
        event_et_et.setHint(hint);

        mPopupWindow = new PopupWindow(view,width,height);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(false);
        if (animationStyle != 0) {
            mPopupWindow.setAnimationStyle(animationStyle);
        }
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if(onDismissListener!=null){
                    onDismissListener.onDismiss();
                }
                SociaxUIUtils.hideSoftKeyboard(activity,event_et_et);
                setWindowNormal();
            }
        });

    }

    protected void setWindowNormal() {
        setWindowAlpha(1.0f);
    }

    private void setWindowAlpha(float alpha) {
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = alpha;
        params.verticalMargin = 100;
        activity.getWindow().setAttributes(params);
    }

    /**
     * 显示popWindow :相对于控件
     */
    @TargetApi(19)
    public void showPopAsDropDown(View parent, int offX, int offY, int gravity) {
        setWindowAlpha();
        // 设置popwindow显示位置
        try {
            mPopupWindow.showAsDropDown(parent, offX, offY, gravity);
        }catch (Throwable t){
            mPopupWindow.showAsDropDown(parent, offX, offY);
        }
        if (Build.VERSION.SDK_INT != 24)
            mPopupWindow.update();
        //startAnim();
    }

    public void showPopAsLocation(View parent, int offX, int offY, int gravity){
        setWindowAlpha();
        // 设置popwindow显示位置
        try {
            mPopupWindow.showAtLocation(parent, gravity, offX, offY);
        }catch (Throwable t){
            mPopupWindow.showAtLocation(parent, Gravity.CENTER, offX, offY);
        }
        if (Build.VERSION.SDK_INT != 24)
            mPopupWindow.update();
    }


    /**
     * 显示popWindow,在屏幕的最下方
     */
    public void showPopAtDown(View parent) {
        setWindowAlpha();
        // 设置popwindow显示位置
        mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        if (Build.VERSION.SDK_INT != 24)
            mPopupWindow.update();
    }

    protected void setWindowAlpha() {
        setWindowAlpha(0.8f);
    }

    public void dismiss(){
        mPopupWindow.dismiss();
    }


    public interface OnPopConfirm{
        void onConfirm(String et_content);
    }

    public interface OnPopCancel{
        void onCancel();
    }

    public interface OnDismissListener{
        void onDismiss();
    }

    public static class Builder{
        private EventEditPopupWindow popupWindow;
        private Activity activity;
        private String title = "";
        private String hint = "";
        private String confirmText = "确定";
        private String cancelText = "取消";
        private OnPopConfirm onPopConfirm = new OnPopConfirm() {
            @Override
            public void onConfirm(String et_content) {
                popupWindow.dismiss();
            }
        };
        private OnPopCancel onPopCancel = new OnPopCancel() {
            @Override
            public void onCancel() {
                popupWindow.dismiss();
            }
        };
        private OnDismissListener onDismissListener = new OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        };
        private int animationStyle = 0;
        private int width = ViewGroup.LayoutParams.WRAP_CONTENT;
        private int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        private int inputType = InputType.TYPE_CLASS_TEXT;
        private String content ="";
        private boolean isEdit = true;

        public Builder(Activity mactivity){
            activity = mactivity;
        }

        public Builder setTittle(String s){
            this.title = s;
            return this;
        }

        public Builder sethint(String s){
            this.hint = s;
            return this;
        }

        public Builder setConfirmText(String s){
            this.confirmText = s;
            return this;
        }

        public Builder setCancelText(String s){
            this.cancelText = s;
            return this;
        }

        public Builder setOnPopConfirm(OnPopConfirm s){
            this.onPopConfirm = s;
            return this;
        }

        public Builder setOnPopConfirm(OnPopCancel s){
            this.onPopCancel = s;
            return this;
        }

        public Builder setAnimationStyle(int s){
            this.animationStyle = s;
            return this;
        }

        public Builder setWidth(int s){
            this.width = s;
            return this;
        }

        public Builder setHeight(int s){
            this.height = s;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener listener){
            this.onDismissListener = listener;
            return this;
        }

        public Builder setInputType(int s){
            inputType = s;
            return this;
        }

        public Builder setContent(String s){
            content = s;
            return this;
        }


        public Builder setIsEdit(boolean s){
            isEdit = s;
            return this;
        }


        public EventEditPopupWindow build(){
            popupWindow = new EventEditPopupWindow(this);
            return popupWindow;
        }
    }

}
